package com.southminority.ethnic.controller.research;

import com.southminority.ethnic.common.result.R;
import com.southminority.ethnic.service.research.ResearchProgressService;
import com.southminority.ethnic.controller.research.vo.ResearchProgressVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 研究进展展示接口
 * 
 * @author SouthMinority
 */
@Slf4j
@Tag(name = "研究进展管理", description = "研究进展展示相关接口")
@RestController
@RequestMapping("/api/research/progress")
@CrossOrigin
public class ResearchProgressController {

    private final ResearchProgressService researchProgressService;

    public ResearchProgressController(ResearchProgressService researchProgressService) {
        this.researchProgressService = researchProgressService;
    }

    @GetMapping("/list")
    @Operation(summary = "获取研究进展列表")
    public R<List<ResearchProgressVO>> getResearchProgressList(
            @Parameter(description = "归属机构ID") @RequestParam(required = false) Long organizationId) {
        try {
            List<ResearchProgressVO> progressList = researchProgressService.getResearchProgressList(organizationId);
            log.info("获取研究进展列表成功，共{}项进展", progressList.size());
            return R.ok(progressList);
        } catch (Exception e) {
            log.error("获取研究进展列表失败", e);
            return R.fail("获取研究进展列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/timeline")
    @Operation(summary = "获取时间线展示的研究进展列表")
    public R<List<ResearchProgressVO>> getTimelineResearchProgressList(
            @Parameter(description = "归属机构ID") @RequestParam(required = false) Long organizationId) {
        try {
            List<ResearchProgressVO> progressList = researchProgressService.getTimelineResearchProgressList(organizationId);
            log.info("获取时间线研究进展列表成功，共{}项进展", progressList.size());
            return R.ok(progressList);
        } catch (Exception e) {
            log.error("获取时间线研究进展列表失败", e);
            return R.fail("获取研究进展列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/featured")
    @Operation(summary = "获取重点展示的研究进展列表")
    public R<List<ResearchProgressVO>> getFeaturedResearchProgressList(
            @Parameter(description = "归属机构ID") @RequestParam(required = false) Long organizationId) {
        try {
            List<ResearchProgressVO> progressList = researchProgressService.getFeaturedResearchProgressList(organizationId);
            log.info("获取重点研究进展列表成功，共{}项进展", progressList.size());
            return R.ok(progressList);
        } catch (Exception e) {
            log.error("获取重点研究进展列表失败", e);
            return R.fail("获取研究进展列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/recent")
    @Operation(summary = "获取最近的研究进展")
    public R<List<ResearchProgressVO>> getRecentResearchProgress(
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "5") Integer limit,
            @Parameter(description = "归属机构ID") @RequestParam(required = false) Long organizationId) {
        try {
            List<ResearchProgressVO> progressList = researchProgressService.getRecentResearchProgress(limit, organizationId);
            log.info("获取最近研究进展成功，共{}项进展", progressList.size());
            return R.ok(progressList);
        } catch (Exception e) {
            log.error("获取最近研究进展失败", e);
            return R.fail("获取研究进展失败: " + e.getMessage());
        }
    }

    @GetMapping("/detail/{id}")
    @Operation(summary = "获取研究进展详情")
    public R<ResearchProgressVO> getResearchProgressDetail(
            @Parameter(description = "研究进展ID") @PathVariable Long id) {
        try {
            ResearchProgressVO progressVO = researchProgressService.getResearchProgressDetail(id);
            log.info("获取研究进展详情成功，进展ID: {}", id);
            return R.ok(progressVO);
        } catch (Exception e) {
            log.error("获取研究进展详情失败，进展ID: {}", id, e);
            return R.fail("获取研究进展详情失败: " + e.getMessage());
        }
    }

    @GetMapping("/by-project/{projectId}")
    @Operation(summary = "根据项目ID获取研究进展列表")
    public R<List<ResearchProgressVO>> getResearchProgressByProject(
            @Parameter(description = "项目ID") @PathVariable Long projectId) {
        try {
            List<ResearchProgressVO> progressList = researchProgressService.getResearchProgressByProject(projectId);
            log.info("根据项目获取研究进展列表成功，项目ID: {}，共{}项进展", projectId, progressList.size());
            return R.ok(progressList);
        } catch (Exception e) {
            log.error("根据项目获取研究进展列表失败，项目ID: {}", projectId, e);
            return R.fail("获取研究进展列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/by-field")
    @Operation(summary = "根据研究领域获取研究进展列表")
    public R<List<ResearchProgressVO>> getResearchProgressByField(
            @Parameter(description = "研究领域") @RequestParam String researchField) {
        try {
            List<ResearchProgressVO> progressList = researchProgressService.getResearchProgressByField(researchField);
            log.info("根据研究领域获取研究进展列表成功，领域: {}，共{}项进展", researchField, progressList.size());
            return R.ok(progressList);
        } catch (Exception e) {
            log.error("根据研究领域获取研究进展列表失败，领域: {}", researchField, e);
            return R.fail("获取研究进展列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/test")
    @Operation(summary = "测试接口")
    public R<String> test() {
        return R.ok("研究进展服务正常");
    }
} 
